package MyLinkedList;

import java.util.List;

public class myLinkList {
    public class ListNode{
        public ListNode next;
        public ListNode prev;
        public int val;
        public ListNode(int val){
            this.val=val;
        }
    }
    public ListNode head;
    public ListNode last;

    //头部插入
    public void addFist(int val){
        ListNode node=new ListNode(val);
        if(head==null){
            head=last=node;
        }else{
            node.next=head;
            head.prev=node;
            head=node;
        }
    }
    //尾部插入
    public void addList(int val){
        ListNode node =new ListNode(val);
        if(head==null){
            head=last=node;
        }
        last.next=node;
        node.prev=last;
        last=node;
    }
    //指定位置位置插入
    public void add(int index,int val){

        if(index==0){
            addFist(val);
        }
        if(index==size()){
            addList(val);
        }
        try{
            checkIndex(index);
        }catch (indexExceptionCheck e){
            e.printStackTrace();
        }
        //找到指定的节点
        ListNode cur=findIndex(index,val);
        ListNode node =new ListNode(val);
        node.prev=cur;
        node.next=cur.next;
        cur.next.prev=node;
        cur.next=node;
    }
    public ListNode findIndex(int index,int val){
        ListNode cur=head;
        int count=0;
        while(count!=index-1){
            cur=cur.next;
            count++;
        }
        return cur;

    }
    private void checkIndex(int index){
        if(index<0||index>size()){
            throw new RuntimeException("index位置不合法");
        }
    }
    public boolean contains(int val){
        ListNode cur=head;
        if(head==null){
            return false;
        }
        while(cur!=null){
            if(cur.val==val){
                return true;
            }
        }
        return false;
    }
    public int size(){
        int count=0;
        ListNode cur=head;
        while (cur!=null){
            cur=cur.next;
            count++;
        }
        return count;
    }
    public void remove(int val){
        if(head==null){
            return;
        }
        ListNode cur=head;
        while(cur!=null){
            if(cur.val==val){
                break;
            }
            cur=cur.next;
        }
        cur.prev.next=cur.next;
        ListNode del=cur;
        del.next.prev=del.prev;
    }
    public void removeAllKey(int val){
        if(head==null){
            System.out.println("链表为空,没有元素可以删除");
            return;
        }
        ListNode low=head;
        ListNode fast=head.next;
        while(low!=null){
            if(fast.val==val){
                low.next=fast.next;
            }else{
                low=fast;
            }
            low=low.next;
        }
        if(head.val==val){
            head=head.next;
        }
    }
    public void clear(){
        head=null;
    }
    public void reverse(){
        if(head==null){
            return;
        }
        ListNode low=head;
        ListNode fast=head.next;
        while(low!=null){


        }
    }



}
